<?php

namespace App\Http\Controllers;

use App\Course;
use App\CourseComment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CourseController extends Controller
{
    //列表
    public function index(Request $request){
        $category_id=$request->category_id;
        return view('web.course.index',compact('category_id'));
    }
    //
    public  function get_more(Request $request){
        $category_id=$request->category_id;
        $course=Course::query();

        if($category_id || $category_id==='0'){

            $course=$course->where('category_id',$category_id);
        }
        $config=config('admin.writing');
        $course=$course->orderBy('id','desc')->paginate(10);
        return view('web.course.get_more',compact('course','config'));
    }
    //上传
    public function course_upload(Request $request){
        $config=config('admin.writing');
        return view('web.course.upload',compact('config'));
    }
    public function course_post(Request $request){
        if(Auth::user()->allow_comment==0){
            return redirect()->back()->with(
                ['error'=>trans('web.comment_permission')]
            );
        }
        $this->validate($request,[
            'title'=>'required',
            'category_id'=>'required',
            'content'=>'required'
            ]);
        $course=new Course();
        $course->title=$request->title;
        $course->category_id=$request->category_id;
        $course->content=$request->input('content');
        $course->user_id=Auth::id();
        $course->save();
        return redirect()->route('course_list')->with(
            [
                'status'=>trans('web.success')
            ]
        );

    }
    public function detail(Request $request){
        $detail=Course::with(['user','comment'=>function($query){
            return $query->where('is_display',1)
                ->orderBy('id','desc');
        }])->findOrFail($request->id);
        return view('web.course.detail',compact('detail'));
    }
    public function comment_post(Request $request){
        if(Auth::user()->allow_comment==0){
            return redirect()->back()->with(
                ['error'=>trans('web.comment_permission')]
            );
        }
        $this->validate($request,[
            'content'=>'required',
        ]);

        $replay_user_id=$request->replay_user_id;
        $course_id=$request->course_id;
        $user_id=Auth::id();
        $content=$request->input('content');
        $comment=new CourseComment();
        $comment->replay_user_id=$replay_user_id;
        $comment->course_id=$course_id;
        $comment->user_id=$user_id;
        $comment->content=$content;
        $comment->save();
        $course=Course::findOrFail($course_id);
        $course->increment('comments',1);
        return redirect()->back()->with(
            [
                'status'=>trans('web.comment_success')
            ]
        );

    }
    public function comment_del(Request $request){
        $comment_id=$request->comment_id;
        $courseComment=CourseComment::find($comment_id);
        $courseComment->is_display=0;
        $courseComment->save();
        $course=Course::findOrFail($courseComment->course_id) ;
        $course->decrement('comments',1);
        return redirect()->back()->with(
            ['status'=>trans('web.displayed')]
        );
    }
    public function course_del(Request $request){
        $course_id=$request->course_id;
        $course=Course::findOrFail($course_id);
        CourseComment::where('course_id',$course_id)->delete();
        $course->delete();
        return redirect()->route('course_list')->with(
            ['status'=>trans('web.writing_del')]
        );

    }
}
